/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.web.common.component.table;

/**
 *
 * @author DaiMX
 */
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;

public abstract class AbstractColumnComparator
        implements Comparator {

    protected String m_attributeName;
    protected boolean m_isAscending;

    protected AbstractColumnComparator(String pm_attributeName, boolean pm_isAscending) {
        this.m_attributeName = pm_attributeName;
        this.m_isAscending = pm_isAscending;
    }

    private Object getValue(Object pm_source, String pm_methodName)
            throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        try {
            Method methodUi = pm_source.getClass().getMethod("getUi", new Class[0]);
            pm_source = methodUi.invoke(pm_source, new Object[0]);
        } catch (Exception e) {
        }

        try {
            return pm_source.getClass().getMethod("get" + pm_methodName, new Class[0]).invoke(pm_source, new Object[0]);
        } catch (Exception e2) {
        }
        return pm_source.getClass().getMethod("is" + pm_methodName, new Class[0]).invoke(pm_source, new Object[0]);
    }

    public int compare(Object pm_o1, Object pm_o2) {
        try {
            Object valueSource = getValue(pm_o1, this.m_attributeName);
            Object valueTarget = getValue(pm_o2, this.m_attributeName);

            return compareValue(valueSource, valueTarget);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public abstract int compareValue(Object paramObject1, Object paramObject2);
}
